package com.wang.helloworld.hellojetty.controllers;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Controller, which is loaded by WebMvcConfig class to be serviced as a part of DispatcherServlet
 */
@Controller
public class HomeController {
	@RequestMapping(value = "/home", method = RequestMethod.GET)
    @ResponseBody
    public String showIndex() {
        return "Hello Jetty!";
    }
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(@RequestParam(value="error", required=false) boolean error, ModelMap model) 
	{
		if (error == true) 
		{
			model.put("error", "You have entered an invalid username or password!");
		}
		else
		{
			model.put("error", "");
		}
		
		return "/pub/login";
 	}
	
	@RequestMapping(value = "/admin/console", method = RequestMethod.GET)
    @ResponseBody
    public String showAdminConsole() {
        return "Hello Admin!";
    }
	
	@RequestMapping(value = "/secret", method = RequestMethod.GET)
	@PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public String showSecret() {
        return "You find a secret!";
    }

}
